package com.starlight.flower_server.infrastructure.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.starlight.flower_server.controller.exception.NotFoundException;
import com.starlight.flower_server.domain.comment.cqrs.CommentPageQuery;
import com.starlight.flower_server.infrastructure.entity.Comment;
import com.starlight.flower_server.infrastructure.mapper.CommentMapper;
import com.starlight.flower_server.infrastructure.util.CurrentUserInfoUtils;
import com.starlight.flower_server.infrastructure.util.PageUtils;
import com.starlight.flower_server.infrastructure.repository.CommentRepository;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Author yfy2001
 * @date 2023/4/17 20:07
 */
@Repository
public class CommentRepositoryImpl implements CommentRepository {
    @Resource
    private CommentMapper commentMapper;


    @Override
    public Comment queryDetail(Long id) throws NotFoundException {
        return commentMapper.selectById(id);
    }

    @Override
    public Integer logicDelete(Long id) {
        return commentMapper.deleteById(id);
    }

    @Override
    public Integer insert(Comment comment) {
        String operator= CurrentUserInfoUtils.get("username");
        comment.setCreator(operator);
        comment.setModifier(operator);
        comment.setCreated(new Date());
        comment.setModified(new Date());
        comment.setIsDeleted(0);
        return commentMapper.insert(comment);
    }

    @Override
    public Integer modify(Comment comment) {
        comment.setModified(new Date());
        return commentMapper.updateById(comment);
    }

    @Override
    public IPage<Comment> queryPage(CommentPageQuery query) {
        QueryWrapper<Comment> queryWrapper= PageUtils.createOrderQuery(query);
        queryWrapper.lambda()
                .isNotNull(Comment::getId)
                .eq(Objects.nonNull(query.getId()),Comment::getId,query.getId())
                .eq(Objects.nonNull(query.getFlowerUserId()),Comment::getFlowerUserId,query.getFlowerUserId())
                .eq(Objects.nonNull(query.getArticleId()),Comment::getArticleId,query.getArticleId());
        return commentMapper.selectPage(PageUtils.startPage(query),queryWrapper);
    }

    @Override
    public List<Comment> queryAll() {
        return commentMapper.selectList(new LambdaQueryWrapper<Comment>()
                .isNotNull(Comment::getId));
    }

    @Override
    public Integer removeAllByArticleId(Long articleId) {
        return commentMapper.delete(new LambdaQueryWrapper<Comment>()
                .eq(Comment::getArticleId,articleId));
    }

    @Override
    public Long queryTotalCount() {
        return commentMapper.selectCount(new LambdaQueryWrapper<Comment>()
                .isNotNull(Comment::getId));
    }

    @Override
    public List<Comment> queryListByArticleId(Long articleId) {
        return commentMapper.selectList(new LambdaQueryWrapper<Comment>()
                .eq(Comment::getArticleId,articleId));
    }

}
